package com.example.sixteen.jt.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sixteen.jt.R;

import java.util.ArrayList;
import java.util.List;

public class ListViewAdapter extends BaseAdapter {

    private static final String TAG = "XXX_ListViewAdapter";

    private LayoutInflater layoutInflater;
    private List<EpcBean> epcBeanArrayList = new ArrayList<>();
    private Context context;

    public ListViewAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<EpcBean> epcBeans) {
        if (epcBeans == null) {
            epcBeanArrayList.clear();
        } else {
            epcBeanArrayList = new ArrayList<>(epcBeans);
        }
        this.notifyDataSetChanged();
    }

    public void select(int index) {
        epcBeanArrayList.get(index).setSelected(true);
        this.notifyDataSetChanged();
    }

    public void deselect() {
        for (EpcBean epcBean : epcBeanArrayList) {
            epcBean.setSelected(false);
        }
        this.notifyDataSetChanged();
    }

    public void deselect(int index) {
        epcBeanArrayList.get(index).setSelected(false);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return epcBeanArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return epcBeanArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public List<EpcBean> getSelected() {
        List<EpcBean> list = new ArrayList<>();
        for (EpcBean bean : epcBeanArrayList) {
            if (bean.isSelected()) {
                list.add(bean);
            }
        }
        return list;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = layoutInflater.inflate(R.layout.item_inventory, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.checkSel = view.findViewById(R.id.check_sel);
            viewHolder.textViewIndex = view.findViewById(R.id.text_view_index);
            viewHolder.textViewEpc = view.findViewById(R.id.text_view_epc);
            viewHolder.textViewCount = view.findViewById(R.id.text_view_count);
            viewHolder.textViewAnt = view.findViewById(R.id.text_view_ant);
            viewHolder.textViewBox = view.findViewById(R.id.text_view_box);
            viewHolder.layoutItem = view.findViewById(R.id.layout_item);
            view.setTag(viewHolder);
        }
        viewHolder = (ViewHolder) view.getTag();
        viewHolder.textViewIndex.setText(String.valueOf(i + 1));
        viewHolder.textViewEpc.setText(epcBeanArrayList.get(i).getEpc());
        viewHolder.textViewCount.setText(String.valueOf(epcBeanArrayList.get(i).getCount()));
        viewHolder.textViewAnt.setText(String.valueOf(epcBeanArrayList.get(i).getAnt()));
        viewHolder.textViewBox.setText(String.valueOf(epcBeanArrayList.get(i).getBox()));
        int resId = epcBeanArrayList.get(i).isSelected() ? R.color.purple_200 : android.R.color.transparent;
        viewHolder.layoutItem.setBackgroundColor(context.getResources().getColor(resId));
        viewHolder.checkSel.setChecked(epcBeanArrayList.get(i).isSelected());
        return view;
    }

    class ViewHolder {
        public TextView textViewIndex, textViewEpc, textViewCount, textViewAnt, textViewBox;
        public LinearLayout layoutItem;
        public CheckBox checkSel;
    }
}
